package com.ruoyi.web.controller.otherPlatform;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.OrderType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.smy.SecurityWebUser;
import com.ruoyi.common.dto.MerchantDto;
import com.ruoyi.system.service.smy.ISecurityWebUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 宴请天下暴露接口
 * @classname BanquetController
 * @author ZhanCheng.Li
 * @date 2023/8/8 15:30
 * @version 1.0.0
 */
@RestController
@RequestMapping(value = "/banquet")
public class BanquetController extends BaseController {

	@Resource
	private ISecurityWebUserService securityWebUserService;


	/**
	 * 查询
	 * @Date 2023/8/8 16:04
	 * @author ZhanCheng.Li
	 * @param
	 * @return
	 */
	@PostMapping(value = "/getBanquetMerchant")
	public AjaxResult getBanquetMerchant(@RequestBody MerchantDto merchantDto) {
		SecurityWebUser queryDto = new SecurityWebUser();
		if (StrUtil.isNotEmpty(merchantDto.getProvince())
				&& StrUtil.isNotEmpty(merchantDto.getCity())
				&& StrUtil.isNotEmpty(merchantDto.getArea())) {
			queryDto.setProvinceName(merchantDto.getProvince());
			queryDto.setCityName(merchantDto.getCity());
			queryDto.setCountyName(merchantDto.getArea());
		} else {
			return AjaxResult.success();
		}
		//查询启用中的饭店商家
		queryDto.setGroupType(OrderType.RESTAURANT);
		queryDto.setRoleId(4L);
		queryDto.setAccountnonlocked(1);
		startPage();
		List<SecurityWebUser> merchantList = securityWebUserService.selectSecurityWebUserList(queryDto);
		int startIndex = (merchantDto.getPageNum() - 1) * merchantDto.getPageSize();
		int endIndex = merchantDto.getPageNum() * merchantDto.getPageSize();
		List<SecurityWebUser> userList;
		if (endIndex > merchantList.size()) {
			userList = merchantList.subList(startIndex, merchantList.size());
		} else {
			userList = merchantList.subList(startIndex, endIndex);
		}
		List<MerchantDto> dtoList = buildMerchantDtoList(userList);
		return AjaxResult.success(dtoList);
	}

	private List<MerchantDto> buildMerchantDtoList(List<SecurityWebUser> userList) {
		List<MerchantDto> resultList = new ArrayList<>();
		if (CollectionUtil.isNotEmpty(userList)) {
			userList.forEach(item -> {
				MerchantDto merchantDto = new MerchantDto();
				merchantDto.setShopName(item.getShopname());
				merchantDto.setMobile(item.getMobile());
				merchantDto.setShowPic(item.getShowPic());
				merchantDto.setChannelStr(item.getChannelStr());
				merchantDto.setRateStar(item.getRatestar());
				resultList.add(merchantDto);
			});
		}
		return resultList;
	}


}
